package ETL

import (
	"fmt"
	"github.com/godror/godror"
	"db2s/ref"
)

type GisPointStr struct {
	X any `json:"X"`
	Y any `json:"Y"`
	Z any `json:"Z"`
}

func getGodRorGisSRid(val any) (sRid string) {
	switch val.(type) {
	case map[string]any:
		if newVal, ok := val.(map[string]any)["SDO_SRID"]; ok {
			sRid = fmt.Sprintf("%v", newVal)
		}
	}
	return
}
func getGodRorGisAttributeMapVal(val *godror.Object) (newVal map[string]any, err error) {
	newVal = make(map[string]any)
	if val == nil {
		return
	}
	for _, v := range val.AttributeNames() {
		var pp any
		if pp, err = val.Get(v); err != nil {
			return
		}
		newVal[v] = pp
	}
	return
}
func gisPoint(gType godror.Number, val any) (wkt any, err error) {
	switch gType {
	case "2001", "3001":
		var sRid = getGodRorGisSRid(val)
		var point string
		newVal, ok := val.(map[string]any)["SDO_POINT"]
		if !ok {
			return
		}
		switch newVal.(type) {
		case *godror.Object:
			var qq map[string]any
			if qq, err = getGodRorGisAttributeMapVal(newVal.(*godror.Object)); err != nil {
				return
			}
			var p GisPointStr
			if err = ref.MapToStruct(qq, &p); err != nil {
				return
			}

			point = fmt.Sprintf("%v %v %v", p.X, p.Y, p.Z)
		}
		wkt = fmt.Sprintf("POINT(%v)SRID:%v", point, sRid)
	}
	return
}
func gisMultiPoint(gType godror.Number, val any) (wkt any, err error) {
	switch gType {
	case "2005":
	case "3005":
	}
	return
}

// gisLinestring 二维、三维  线
func gisLinestring(gType godror.Number, val any) (wkt any, err error) {
	fmt.Println("---------pppp:", val)
	switch gType {
	case "2002":
	case "3002":
	}
	return
}
func gisMultiLinestring(gType godror.Number, val any) (wkt any, err error) {
	switch gType {
	case "2006":
	case "3006":
	}
	return
}

// gisPolygon 二维、三维  面
func gisPolygon(gType godror.Number, val any) (wkt any, err error) {
	switch gType {
	case "2003":
	case "3003":
	}
	return
}

// gisPolygon 二维、三维  面
func gisMultiPolygon(gType godror.Number, val any) (wkt any, err error) {
	switch gType {
	case "2007":
	case "3007":
	}
	return
}

// gisPolygon 二维、三维  面
func gisCollection(gType godror.Number, val any) (wkt any, err error) {
	switch gType {
	case "2004":
	case "3004":
	}
	return
}
func godRorObjectSdoGeometry(object *godror.Object) (wkt any, err error) {
	var attribute map[string]any
	fmt.Println("-------heihei:", object)
	if attribute, err = getGodRorGisAttributeMapVal(object); err != nil {
		return
	}
	/*
		godror 不支持 SDO_ORDINATE_ARRAY 多边形写入的方式
		例如：
			INSERT INTO GIS_DATA_TYPES2 (ID,GEOMETRY_POINT)VALUES(2,SDO_GEOMETRY(3001,4326,NULL,SDO_ELEM_INFO_ARRAY(1,1003,3),SDO_ORDINATE_ARRAY(1,1, 5,7)));
		支持：
		INSERT INTO GIS_DATA_TYPES2 (ID, GEOMETRY_POINT) VALUES (13,SDO_GEOMETRY(3001,4326,SDO_POINT_TYPE(13,15,1),NULL,NULL));
	*/
	switch v, _ := attribute["SDO_GTYPE"]; v.(type) {
	case godror.Number:
		switch v.(godror.Number) {
		case "2001", "3001": //SDO_POINT2D(2001),SDO_POINT3D(3001)
			return gisPoint(v.(godror.Number), attribute)
		case "2002", "3002": //SDO_CURVE2D(2002),SDO_LINESTRING2D(2002),SDO_CURVE3D(3002),SDO_LINESTRING3D(3002)
			return gisLinestring(v.(godror.Number), attribute)
		case "2003", "3003": //SDO_POLYGON2D(2003),SDO_POLYGON3D(3003)
			return gisPolygon(v.(godror.Number), attribute)
		case "2004", "3004": //SDO_COLLECTION2D(2004),SDO_COLLECTION3D(3004)
			return gisCollection(v.(godror.Number), attribute)
		case "2005", "3005": //SDO_MULTIPOINT2D(2005),SDO_MULTIPOINT3D(3005)
			return gisMultiPoint(v.(godror.Number), attribute)
		case "2006", "3006": //SDO_MULTICURVE2D(2006),SDO_MULTILINESTRING2D(2006),SDO_MULTICURVE3D(3006),SDO_MULTILINESTRING3D(3006)
			return gisMultiLinestring(v.(godror.Number), attribute)
		case "2007", "3007": //SDO_MULTIPOLYGON2D(2007),SDO_MULTIPOLYGON3D(3007)
			return gisMultiPolygon(v.(godror.Number), attribute)
		default:
			fmt.Println("------rrrqq:", v)
		}
	default:
		fmt.Println("------rrrqq:", v)
	}
	return
}

func colValGodRorObject(object *godror.Object) (val any, err error) {
	switch object.FullName() {
	case "MDSYS.SDO_GEOMETRY":
		return godRorObjectSdoGeometry(object)
	default:

	}
	return
}
